package com.walixiwa.player.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.io.File;

/**
 * 播放位置记录数据库
 */
public class VodPositionManager {
    private static final String dbName = "vodPosition.db";
    private static VodPositionManager instance;
    private static SQLiteDatabase liteDatabase;
    private static final String CREATE_TABLE =
            "CREATE TABLE IF NOT EXISTS vodPosition ( id integer PRIMARY KEY,url text,position integer)";

    public static VodPositionManager getInstance() {
        if (instance == null) {
            instance = new VodPositionManager();
        }
        return instance;
    }

    public static void init(Context context) {
        if (liteDatabase == null) {
            File file = context.getDatabasePath(dbName);
            liteDatabase = context.openOrCreateDatabase(file.getAbsolutePath(), Context.MODE_PRIVATE, null);
            liteDatabase.execSQL(CREATE_TABLE);
        }
    }

    public void setPosition(String url, long position) {
        if (getCount() > 200) {
            liteDatabase.execSQL("delete from vodPosition");
        }
        if (isExists(url)) {
            liteDatabase.execSQL("update vodPosition set position = " + position + " where url = '" + url + "'");
        } else {
            liteDatabase.execSQL("insert into vodPosition values (null,'" + url + "'," + position + ")");
        }
    }

    public long getPosition(String url) {
        long position = 0;
        Cursor cursor = null;
        try {
            cursor = liteDatabase.rawQuery("SELECT * FROM vodPosition where url = '" + url + "'", null);
            if (cursor.moveToFirst()) {
                do {
                    position = cursor.getLong(2);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if ((null != cursor) && (!cursor.isClosed())) {
                cursor.close();
            }
        }
        return position;
    }

    public boolean isExists(String url) {
        int count = 0;
        try {
            if (liteDatabase != null) {
                Cursor cursor = liteDatabase.rawQuery("SELECT count(*) FROM vodPosition WHERE url = '" + url + "'", null);
                if (cursor.moveToNext()) {
                    count = cursor.getInt(0);
                }
                cursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count != 0;
    }

    public int getCount() {
        int count = 0;
        try {
            if (liteDatabase != null) {
                Cursor cursor = liteDatabase.rawQuery("SELECT count(*) FROM vodPosition", null);
                if (cursor.moveToNext()) {
                    count = cursor.getInt(0);
                }
                cursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
}
